2013-06-04 61 views
-1

我正在嘗試做一個Simultaneous方程求解器。但我寫的代碼到目前爲止給了我無法實例化Componentinfo NullPointerException的錯誤。我不知道我在哪裏空引用。無法實例化Componentinfo NullPointerException 3

這裏是我的MainActivity.java

package com.dulanga.solver; 

import android.os.Bundle; 
import android.app.Activity; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.Menu; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class MainActivity extends Activity implements TextWatcher { 
EditText unknowns; 
TableRow equationRows; 
LinearLayout mainLayout; 
TextView eqnNumber; 
EditText eqn; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


} 
public void onResume(){ 
    super.onResume(); 

    equationRows=new TableRow(this); 
    eqnNumber=new TextView(this); 
    eqn=new EditText(this); 
    unknowns =(EditText) findViewById(R.id.unknowns); 
    unknowns.addTextChangedListener(this); 
    mainLayout=(TableLayout)findViewById(R.id.LinearLayout1); 
    equationRows.addView(eqn,1); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public void afterTextChanged(Editable arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
     int arg3) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
    // TODO Auto-generated method stub 
    int number=Integer.parseInt(arg0.toString()); 
    mainLayout.removeViews(1, mainLayout.getChildCount()-1); 
    //equationRows.removeAllViews(); 
    for(int i=0;i<number;i++){ 
     if (i==0) 
      eqnNumber.setText((i+1)+"st"); 
     else if (i==1) 
      eqnNumber.setText((i+1)+"nd"); 
     else if (i==2) 
      eqnNumber.setText((i+1)+"rd"); 
     else 
      eqnNumber.setText((i+1)+"th"); 
     equationRows.addView(eqnNumber,0); 
     mainLayout.addView(equationRows); 
     equationRows.removeViewAt(0); 


    } 



} 

} 

AndroidManifest.xml中

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

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.dulanga.solver.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> 
</application> 

</manifest> 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" > 





     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="No. of Unknowns" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:paddingRight="25dp" /> 

      <EditText 
       android:id="@+id/unknowns" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="10" 
       android:inputType="number" > 

       <requestFocus /> 
      </EditText> 

     </TableRow> 




</LinearLayout> 

請誰能幫助我。

+0

,其中充滿了logcat的日誌? – Selvin

+0

對不起,我忘了包括這一點。但無論如何非常感謝您的幫助。我解決了我的問題 – Dulanga

回答

1

刪除

<requestFocus />activity_main.xml

+0

感謝您的幫助。它在刪除後工作 – Dulanga

相關問題