-1

I am trying to make a Simultaneous equation solver. But my code i wrote so far gives me the error Unable to instantiate Componentinfo NullPointerException. I don't know where I am null referencing.

Here's my 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>

Please can anyone help me.

4

1 回答 1

1

消除

<requestFocus />在你的activity_main.xml

于 2013-06-04T15:24:20.073 回答