2013-05-20 5 views
0

SQLView라는 MainActivity와 두 번째 활동이 있습니다. 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 :

,617,
<?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! 
+1

SQLView에서 setContentView (R.layout.mylayout)가 누락되었습니다. mylayout.xml을 정의하고 textview를 정의해야합니다. setContent to SQlView는 텍스트 뷰를 초기화하고 텍스트를 텍스트 뷰로 설정합니다. – 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에서 textview를 android : id = "@ + id/tvSQLinfo"로 사용하려고 시도했습니다. – Raghunandan

+1

@Raghunandan :'tvSQLinfo'를 볼 수 있듯이 textView는'activity_main' 대신'sqlview' 안에 있습니다. 액티비티와 레이아웃 모두 다르다 –

+0

네가 옳다. 나는 같은 것을 발견하는 것을 놓쳤다. +1 – Raghunandan

관련 문제