2016-10-30 40 views
0

マニフェストsetOnClickListener(this)原因エラー?

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.user.foody"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".RestaurantDetail"></activity> 
</application> 
</manifest> 

主な活動

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, 
                  android.view.View.OnClickListener { 
private boolean viewIsAtHome; // 每次 press back 都回到 about(初始頁面) 
Button buttonEditAddYes,buttonEditUorD; 
TextView restaurantID; 

// ----------- 每當按下Button按下,則跑到OnClick()執行對應Function ----------- 

@override 
public void onClick(View view) { 
    if (view== findViewById(R.id.buttonEditAddYes)){ // 確定新增 (Edit_Add內) 

     Intent intent = new Intent(MainActivity.this,RestaurantDetail.class); 
     intent.putExtra("restaurantID",0); 
     startActivity(intent); 
    } 

    else if (view== findViewById(R.id.buttonEditAddNo)){ // 清除 (Edit_Add內) 
    } 

    else if (view== findViewById(R.id.buttonEditUorD)) { // 當按下 (Edit內) 

     RestaurantRepo repo = new RestaurantRepo(this); 

     ArrayList<HashMap<String, String>> restaurantList = repo.getRestaurantList(); 
     if(restaurantList.size()!=0) { 
      ListView lv = (ListView) findViewById(R.id.list); 
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
        restaurantID = (TextView) view.findViewById(R.id.restaurantID); 
        String idRestaurant = restaurantID.getText().toString(); 
        Intent objIndent = new Intent(getApplicationContext(),RestaurantDetail.class); 
        objIndent.putExtra("restaurantID", Integer.parseInt(idRestaurant)); 
        startActivity(objIndent); 
       } 
      }); 
      ListAdapter adapter = new SimpleAdapter(MainActivity.this,restaurantList, R.layout.edit_fragment_fix_view, 
        new String[] { "id","name","type","price","phone","addr"}, 
        new int[] {R.id.restaurantID, R.id.restaurantNAME,R.id.restaurantTYPE, 
          R.id.restaurantPRICE,R.id.restaurantPHONE,R.id.restaurantADDR}); 
      lv.setAdapter(adapter); 
     }else{ 
      Toast.makeText(this,"No restaurant!",Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
// ------------------------------------------------------------------------ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setItemIconTintList(null); // 顯示Icon圖案,不會讓他黑黑的 
    navigationView.setNavigationItemSelectedListener(this); 
    displayView(R.id.menu_about); // 一開始看About 

    // -------------------- 新增/修改/刪除 -------------------- 

    buttonEditAddYes = (Button)findViewById(R.id.buttonEditAddYes); 
    buttonEditAddYes.setOnClickListener(this);// !!!!!!!!!!!!!!!!!!!!!! Here got error 

    buttonEditUorD = (Button)findViewById(R.id.buttonEditUorD);  
    buttonEditUorD.setOnClickListener(this); // !!!!!!!!!!!!!!!!!!!!!! because of ↑ , so this may cause error too 

    // ------------------------------------------------------------ 
} 

RestaurantDetail

public class RestaurantDetail extends Activity implements android.view.View.OnClickListener { 

    Button buttonEditUpdate , buttonEditDelete; 

    EditText editTextNAME; 
    EditText editTextTYPE; 
    EditText editTextPRICE; 
    EditText editTextPHONE; 
    EditText editTextADDR; 
    //EditText editTextSCORE; 

    private int _RestaurantID = 0; 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.edit_fragment_fix_uord); 
      // buttonEditUorD按下時 跳到 edit_fragment_fix_uord去修改 

      buttonEditUpdate = (Button) findViewById(R.id.buttonEditUpdate); 
      buttonEditDelete = (Button) findViewById(R.id.buttonEditUpdate); 

      editTextNAME = (EditText) findViewById(R.id.editTextUpdateName); 
      editTextTYPE = (EditText) findViewById(R.id.editTextUpdateType); 
      editTextPRICE = (EditText) findViewById(R.id.editTextUpdatePrice); 
      editTextPHONE = (EditText) findViewById(R.id.editTextUpdatePhone); 
      editTextADDR = (EditText) findViewById(R.id.editTextUpdateAddr); 
      //editTextSCORE = (EditText) findViewById(R.id.ratingBarUpdate); 

      buttonEditUpdate.setOnClickListener(this); 
      buttonEditDelete.setOnClickListener(this); 

      _RestaurantID =0; 
      Intent intent = getIntent(); 
      _RestaurantID =intent.getIntExtra("restaurantID", 0); 
      RestaurantRepo repo = new RestaurantRepo(this); 
      Restaurant restaurant = new Restaurant(); 
      restaurant = repo.getRestaurantById(_RestaurantID); 

      editTextNAME.setText(restaurant.name); 
      editTextTYPE.setText(String.valueOf(restaurant.type)); 
      editTextPRICE.setText(String.valueOf(restaurant.price)); 
      editTextPHONE.setText(restaurant.phone); 
      editTextADDR.setText(restaurant.addr); 
      //editTextSCORE.setText(String.valueOf(restaurant.score)); 
     } 

    @Override 
     public void onClick(View view) { 
      // TODO Auto-generated method stub 
      if (view == findViewById(R.id.buttonEditUpdate)){ 
       RestaurantRepo repo = new RestaurantRepo(this); 
       Restaurant restaurant = new Restaurant(); 

       restaurant.restautant_ID =_RestaurantID; 
       restaurant.name = editTextNAME.getText().toString(); 
       restaurant.type = Integer.parseInt(editTextTYPE.getText().toString()) ; 
       restaurant.price = Integer.parseInt(editTextPRICE.getText().toString()) ; 
       restaurant.phone = editTextPHONE.getText().toString(); 
       restaurant.addr = editTextADDR.getText().toString(); 
       //restaurant.score = Integer.parseInt(editTextSCORE.getText().toString()); 

       if (_RestaurantID==0){ 
        _RestaurantID = repo.insert(restaurant); 

        Toast.makeText(this,"New Restaurant Insert",Toast.LENGTH_SHORT).show(); 
        finish(); 
       } 

       else{ 
        repo.update(restaurant); 
        Toast.makeText(this,"Restaurant Record updated",Toast.LENGTH_SHORT).show(); 
        finish(); 
       } 
      } 

      else if (view== findViewById(R.id.buttonEditDelete)){ 
       RestaurantRepo repo = new RestaurantRepo(this); 
       repo.delete(_RestaurantID); 
       Toast.makeText(this, "Restaurant Record Deleted", Toast.LENGTH_SHORT); 
       finish(); 
      } 
     } 
} 

私はSQLiteデータベースに情報を挿入するボタンを作ることにしました。

ボタン(buttonEditAddYes)をクリックすると、 別のアクティビティ(RestaurantDetail)にパラメータが渡されます。私は多分、私は私のMainActivityにListActivityを拡張するために欠けていると思う

buttonEditAddYes.setOnClickListener(this); 

しかし、私はすでに..

Process: com.example.user.foody, PID: 4577 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.foody/com.example.user.foody.RestaurantDetail}: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.EditText 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:145) 
     at android.app.ActivityThread.main(ActivityThread.java:5942) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
    Caused by: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.EditText 
     at **com.example.user.foody.RestaurantDetail.onCreate(RestaurantDetail.java:39) 
     at** android.app.Activity.performCreate(Activity.java:6288) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)  
     at android.app.ActivityThread.access$900(ActivityThread.java:177)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)  
     at android.os.Handler.dispatchMessage(Handler.java:102)  
     at android.os.Looper.loop(Looper.java:145)  
     at android.app.ActivityThread.main(ActivityThread.java:5942)  
     at java.lang.reflect.Method.invoke(Native Method)  
     at java.lang.reflect.Method.invoke(Method.java:372) 
+0

のonClickメソッドをオーバーライドプレフィックス – SoulRayder

+0

を持っている必要がありますありがとうございました、しかし、私はあなたがあなたの質問にlogcat出力を追加する必要があります@Override – Kevin

+0

を追加するときには敷居エラーが発生しました。 –

答えて

0

をAppCompatActivityを拡張(?):

エラーがこの行で発生しますログは言う:

Caused by: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.EditText 

これらのfindViewByIdはすべてTextViewを参照していますか?おそらくアンドロイドの入力ミスでしょうか?

また、私はあなたがあなたのonClick方法に代わり、IFSの例を使うべきだと思う:

switch(view.getId) { 
    case R.id.your_id: 
    doSomething(); 
    break; 
} 

これは、コードをより読みやすさを提供します;)

0
@Override 
    public void onClick(View view) { 
     // TODO Auto-generated method stub 
     if (view.getId() == R.id.buttonEditUpdate){ 
      RestaurantRepo repo = new RestaurantRepo(this); 
      Restaurant restaurant = new Restaurant(); 

      restaurant.restautant_ID =_RestaurantID; 
      restaurant.name = editTextNAME.getText().toString(); 
      restaurant.type = Integer.parseInt(editTextTYPE.getText().toString()) ; 
      restaurant.price = Integer.parseInt(editTextPRICE.getText().toString()) ; 
      restaurant.phone = editTextPHONE.getText().toString(); 
      restaurant.addr = editTextADDR.getText().toString(); 
      //restaurant.score = Integer.parseInt(editTextSCORE.getText().toString()); 

      if (_RestaurantID==0){ 
       _RestaurantID = repo.insert(restaurant); 

       Toast.makeText(this,"New Restaurant Insert",Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 

      else{ 
       repo.update(restaurant); 
       Toast.makeText(this,"Restaurant Record updated",Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     } 

     else if (view.getId()== R.id.buttonEditDelete){ 
      RestaurantRepo repo = new RestaurantRepo(this); 
      repo.delete(_RestaurantID); 
      Toast.makeText(this, "Restaurant Record Deleted", Toast.LENGTH_SHORT); 
      finish(); 
     } 
    } 

変更

をabouveするためのonClickメソッド
0

基本的にスタックトレースを見てください

java.lang.ClassCastExceptionが:によって引き起こさ

android.widget.TableRowは、それはあなたがオブジェクトの間違った種類をキャストしていることを意味しandroid.widget.EditText

にキャストすることはできません。 RestaurantDetailの39行目を確認してください。あなたのキャスティングTableRow EditTextに!

よろしく

関連する問題