2013-05-20 154 views
0

我得到了一個MainActivity和第二個叫做SQLView的活動。 我嘗試更改SQLView-Activity中TextView的內容。但每次我開始這個活動,我的應用程序崩潰。Android TextView導致崩潰

感謝您的幫助!

的AndroidManifest.xml:

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

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="16" /> 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="com.example.database.SQLView"/> 
    </application> 

</manifest> 

MainActivity:

package com.example.database; 

    import android.content.Intent; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.view.View.OnClickListener; 
    import android.widget.Toast; 


    public class MainActivity extends Activity implements OnClickListener{ 

     Button sqlUpdate, sqlView; 
     EditText sqlName, sqlHotness; 
     private DBHandler entry; 

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

      sqlUpdate = (Button) findViewById(R.id.bSQLUpdate); 
      sqlView = (Button) findViewById(R.id.bSQLView); 

      sqlName = (EditText) findViewById(R.id.edSQLName); 
      sqlHotness = (EditText) findViewById(R.id.edSQLHotness); 

      sqlUpdate.setOnClickListener(this); 
      sqlView.setOnClickListener(this); 

      entry = new DBHandler(MainActivity.this); 

     } 

     public void onClick(View arg0){ 

      switch (arg0.getId()) { 

       case R.id.bSQLUpdate: 
        String name = sqlName.getText().toString(); 
        String hotness = sqlHotness.getText().toString(); 

        entry.insert(name, hotness); 
        entry.close(); 
        Toast.makeText(this, "Eintrag gespeichert", Toast.LENGTH_SHORT).show(); 

        break; 

       case R.id.bSQLView: 
        Toast.makeText(this, "View geklickt", Toast.LENGTH_SHORT).show(); 
        Intent intent = new Intent(MainActivity.this, SQLView.class); 
        startActivityForResult(intent, 0); 
        break; 
      } 
     } 
} 

SQLView:

package com.example.database; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class SQLView extends Activity { 

    private DBHandler dbHandler; 
    TextView tv; 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     tv = (TextView) findViewById(R.id.tvSQLinfo); 

     tv.setText("TEST"); // causes the crash 

    } 
} 

sqlview.xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

    <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <TableRow> 

      <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="Names" 
        android:id="@+id/textView" 
        android:layout_weight="1"/> 
      <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="Hotness" 
        android:layout_weight="1" 
        android:id="@+id/textView2"/> 
     </TableRow> 
    </TableLayout> 

    <TextView 
      android:id="@+id/tvSQLinfo" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="get info from db" /> 
</LinearLayout> 

的logcat:

05-16 14:29:24.864  311-359/system_process     
         E/InputDispatcher: channel '41161ea0                                                                                                                                                   
         com.example.database/com.example.database.MainActivity (server)' 
         ~ Channel is unrecoverably broken and will be disposed! 
+1

您在SQLView中缺少setContentView(R.layout.mylayout)。你應該定義mylayout.xml並定義textview。然後將setContent設置爲SQlView初始化textview並將文本設置爲textview – Raghunandan

回答

1

你忘記onCreate方法來設置佈局SQLView活動。將其設置爲初始化之前TextView

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.sqlview); //set layout here 

      tv = (TextView) findViewById(R.id.tvSQLinfo); 

    } 
+0

他擁有setContentView(R.layout.activity_main);在MainActivity中,並試圖在activity中使用ID爲android:id =「@ + id/tvSQLinfo」的textview – Raghunandan

+1

@Raghunandan:你可以看到'tvSQLinfo' textView在'sqlview'而不是'activity_main'內。活動和佈局是不同的 –

+0

是的,你是對的。我錯過了發現相同。 +1 – Raghunandan