Tuesday, 10 September 2013

Android Layout java.lang.NullPointerException

Android Layout java.lang.NullPointerException

package com.example.scripthelper;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class AddLine extends Activity implements OnClickListener {
private static final String TAG = "myLogs";
Button btnAdd;
Button btnCancel;
EditText camNumberText;
EditText shotNumberText;
EditText descriptionText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_line);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(this);
camNumberText = (EditText) findViewById(R.id.camNumberText);
shotNumberText = (EditText) findViewById(R.id.shotNumberText);
descriptionText = (EditText) findViewById(R.id.descriptionText);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
TableLayout tl = (TableLayout)findViewById(R.id.mainTableLayout);
TableRow row = new TableRow(this);
LinearLayout rowLayout = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText(camNumberText.getText().toString());
LayoutParams marginsRight = new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
marginsRight.setMargins(50, 100, 25, 100);
Log.d(TAG, "Right margin 30");
tv.setLayoutParams(marginsRight);
TextView tv1 = new TextView (this);
tv1.setText(descriptionText.getText().toString());
Log.d(TAG, "Description text good");
//View row =
getLayoutInflater().inflate(R.layout.activity_main, row);
tl.addView(row);
row.addView(rowLayout);
rowLayout.addView(tv);
//rowLayout.addView(tv1);
case R.id.btnCancel:
break;
}
}
@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_add_line, menu);
return true;
}
}
LOGCAT:
09-10 15:01:25.801: E/AndroidRuntime(2758): FATAL EXCEPTION: main
09-10 15:01:25.801: E/AndroidRuntime(2758): java.lang.NullPointerException
09-10 15:01:25.801: E/AndroidRuntime(2758): at
com.example.scripthelper.AddLine.onClick(AddLine.java:64)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
android.view.View.performClick(View.java:4084)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
android.view.View$PerformClick.run(View.java:16966)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
android.os.Handler.handleCallback(Handler.java:615)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
android.os.Looper.loop(Looper.java:137)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
android.app.ActivityThread.main(ActivityThread.java:4745)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
java.lang.reflect.Method.invokeNative(Native Method)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
java.lang.reflect.Method.invoke(Method.java:511)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 15:01:25.801: E/AndroidRuntime(2758): at
dalvik.system.NativeStart.main(Native Method)
i have just started android and was wondering why its causing this error.
I was thinking it might have something to do with the view that is in the
activityMain.xml and not in addLine.xml..
Thanks!

No comments:

Post a Comment