2016-05-15 4 views
2

別のCardViewsをクリックしてアプリケーションの番号をダイヤルしようとしていますが、機能していません。Android CardView OnClickメソッドを使用して電話番号を呼び出す方法

私はインテントを追加しようとするたびに、startActivityが赤に変わり、 "メソッドを解決できません"というエラーメッセージが表示されます。 私はstartActivity()の前にコンテキストを追加しようとしましたが、アプリケーションがクラッシュします。

私はコールと 私は3つのJavaクラスを持って開始するためにクリックする4 CardViews持っている:私はrecycler_viewを使用activity_mainでCardViewDataAdapter recyclerViewホルダーが宣言された、MainActivityとitemObject を。 cardview_rowでは、私はcard_viewを使用し、それをリサイクラビューでバインドしました。

私は親切に私は、この問題を探検してもスタックオーバーフローでそれを見ても解決策はまだ見つかりませんでした。

The error message image

CardViewDataAdapter.javaコード:

public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> { 

    private List<ItemObject> itemList; 

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
     // each data item is a string and an image in this case 
     public ImageView image_view; 
     public TextView image_name; 
     private Context context; 

     public ViewHolder(View itemLayoutView) { 

      super(itemLayoutView); 
      this.image_view = (ImageView) itemLayoutView.findViewById(R.id.image_view); 
      this.image_name = (TextView) itemLayoutView.findViewById(R.id.image_name); 

      // Store the context 
      this.context = context; 
      // Attach a click listener to the entire row view 
      itemLayoutView.setOnClickListener(this); 
     } 

     // Handles the row being being clicked 
     @Override 
     public void onClick(View view) { 

      if (getPosition() == 0){ 
       Intent callIntent = new Intent(Intent.ACTION_DIAL); 
       callIntent.setData(Uri.parse("tel:123456789")); 
       startActivity(callIntent); 
      } else if (getPosition() == 1){ 
       Toast.makeText(view.getContext(), "position2 = " + getPosition(), Toast.LENGTH_SHORT).show(); 
      } else if (getPosition() == 2){ 
       Toast.makeText(view.getContext(), "position3 = " + getPosition(), Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(view.getContext(), "position4 = " + getPosition(), Toast.LENGTH_SHORT).show(); 
      } 
      //context.startActivity(intent); 
     } 
    } 

    // Create new views (invoked by the layout manager) 
    @Override 
    public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
                  int viewType) { 
     // create a new view 
     View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
       R.layout.cardview_row, null); 


     // create ViewHolder 
     ViewHolder viewHolder = new ViewHolder(itemLayoutView); 
     return viewHolder; 
    } 

    // Replace the contents of a view (invoked by the layout manager) 
    @Override 
    public void onBindViewHolder(ViewHolder viewHolder, int position) { 

     // - get data from your itemsData at this position 
     // - replace the contents of the view with that itemsData 
     viewHolder.image_view.setImageResource(itemList.get(position).getPhoto()); 
     viewHolder.image_name.setText(itemList.get(position).getName()); 


    } 

    // Return the size of your dataset (invoked by the layout manager) 
    @Override 
    public int getItemCount() { 
     return this.itemList.size(); 
    } 

    public CardViewDataAdapter(Context context, List<ItemObject> itemList) { 
     this.itemList = itemList; 
    }  
} 

MainActivity.javaコード:

public class MainActivity extends AppCompatActivity { 

    private RecyclerView mRecyclerView; 
    private RecyclerView.Adapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 

     // use this setting to improve performance if you know that changes 
     // in content do not change the layout size of the RecyclerView 
     mRecyclerView.setHasFixedSize(true); 

     // use a GridLayout manager 
     mLayoutManager = new GridLayoutManager(this, 2); 
     mRecyclerView.setLayoutManager(mLayoutManager); 

     // specify an adapter 
     List<ItemObject> rowListItem = getAllItemList(); 
     mAdapter = new CardViewDataAdapter(MainActivity.this, rowListItem); 
     mRecyclerView.setAdapter(mAdapter); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      Toast.makeText(getApplicationContext(), "Settings Clicked", 
        Toast.LENGTH_SHORT).show(); 
      return true; 
     /* } else if (id == R.id.action_search) { 
      Toast.makeText(getApplicationContext(), "Search Clicked", 
        Toast.LENGTH_SHORT).show(); 
      return true;*/ 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    //Handles the references to items displayed on each cards 
    private List<ItemObject> getAllItemList() { 

     List<ItemObject> allItems = new ArrayList<ItemObject>(); 
     allItems.add(new ItemObject("Fire", R.drawable.fire)); 
     allItems.add(new ItemObject("Ambulance", R.drawable.ambulance)); 
     allItems.add(new ItemObject("Police", R.drawable.police)); 
     allItems.add(new ItemObject("AntiSquad", R.drawable.police)); 
     return allItems; 
    } 
} 

itemsObject.javaコード:

public class ItemObject { 

    private String name; 
    private int photo; 

    public ItemObject(String name, int photo) { 
     this.name = name; 
     this.photo = photo; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getPhoto() { 
     return photo; 
    } 

    public void setPhoto(int photo) { 
     this.photo = photo; 
    } 
} 

cardview_row.xmlコード:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:cardview="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 


    <!-- CardView with customized attributes --> 
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="170dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_margin="10dp" 
     cardview:cardCornerRadius="20dp" 
     cardview:cardElevation="90dp" 
     android:elevation="2dp" 
     android:background="@drawable/myrect" 
     > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="8dp"> 

      <ImageView 
       android:id="@+id/image_view" 
       android:layout_width="fill_parent" 
       android:layout_height="120dp" 
       /> 

      <TextView 
       android:id="@+id/image_name" 
       android:layout_width="match_parent" 
       android:layout_height="60dp" 
       android:background="#4757b3" 
       android:textColor="@android:color/white" 
       android:fontFamily="sans-serif-condensed" 
       android:textSize="20sp" 
       android:textAlignment="center" /> 
     </LinearLayout> 

    </android.support.v7.widget.CardView> 

</LinearLayout> 
+0

エラーのエラーやスタックトレースは何ですか? –

+0

クラッシュのスタックトレース全体をあなたの質問の編集として追加すると、より良い返答が得られます。 –

+1

@ShadabKは、OnClick Viewメソッドのif-else部分で 'startActivity(android.content.intent)'メソッドを解決できません –

答えて

1

が見える何かを失ったMBあなたはContext問題を持っているよう

btnTelephone.setOnClickListener(new View.OnClickListener() { 
           @Override 
           public void onClick(View v) { 
            //Call 1st phone number api 
            Uri callIntentUri = Uri.parse("tel:"+telephoneStringForCall); 
            Intent callIntent = new Intent(Intent.ACTION_DIAL, callIntentUri); 
            startActivity(callIntent); 
           } 
          }); 

とは、あなたのビューの初期化を確認してください。

あなたは以下のようにコードを変更する必要があるコンテキストを取得するには:

Intent callIntent = new Intent(Intent.ACTION_DIAL); 
callIntent.setData(Uri.parse("tel:123456789")); 
view.getContext().startActivity(callIntent); 
+1

これはコンテキスト問題ではないようですが、それは間違いなくコンテキスト問題です!ありがとう、それは動作します.....恐ろしい –

+0

このコードはちょうど私と一緒に働いた –

0

はこれを試してください:あなたは

関連する問題