0

我有一个 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>

主要活动:

    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;
            }
        }
}

SQL 视图:

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>

日志猫:

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!
4

1 回答 1

1

您忘记在方法中为SQLViewActivity设置布局。onCreate像初始化之前一样设置它TextView

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

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

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

   }
于 2013-05-20T18:00:47.283 回答