Android TextView导致崩溃

时间:2013-05-20 17:54:14

标签: android textview

我有一个MainActivity和第二个叫做SQLView的活动。 我尝试在SQLView-Activity中更改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 个答案:

答案 0 :(得分:1)

您忘记在SQLView方法中为onCreate活动设置布局。在初始化TextView之前将其设置为:

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

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

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

   }