我的App活动有一个问题...这是我的InsertActivity
代码,我可以从两个活动中找到它。
其中一个来自菜单add new note
,我使用正常的intent
,并且活动为空白以填充EditText
第二个来自编辑注释,其中我使用putExtra意图与点击项目的_id。
我的问题在于onCreate
的底线。在getIntent()
之后以及从if
到完全填充EditText
并使用现有数据进行编辑...当我取消注释if
行时,点击按钮add/edit
后的应用将崩溃单词无法启动活动。
请帮忙(odosli是_id)
public class PridajPoActivity extends Activity
{
public static final String ODOSLI = "odosli";
private DatabaseOp mDbHelper;
String predmet = "";
String obsah = "";
String username;
String odosli = "";
EditText tv_nazov;
EditText tv_poznamka;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poznamka_pridaj);
mDbHelper = new DatabaseOp(this);
mDbHelper.open();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
username = sharedPrefs.getString("prefUsername", "NULL");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Intent i = getIntent();
odosli = i.getStringExtra(ODOSLI);
if (odosli.length()>0)
{
dbOperations();
tv_nazov = (EditText)findViewById(R.id.predmet);
tv_nazov.setText(predmet);
tv_poznamka = (EditText)findViewById(R.id.obsah);
tv_poznamka.setText(obsah);
}
}
protected void onPause(Bundle savedInstanceState)
{
mDbHelper.close();
}
public void dbOperations ()
{
mDbHelper = new DatabaseOp(this);
mDbHelper.open();
Cursor cursor = mDbHelper.fetchNote(odosli);
startManagingCursor(cursor);
predmet = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));
obsah = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_POZNAMKA));
}
public void buttonClicked_uloz(View button)
{
getData();
if (predmet.length()<1)
{
Toast.makeText(this, R.string.invalid_nazov, Toast.LENGTH_SHORT).show();
} else {
if (odosli.length()>0)
{
mDbHelper.updateNote(odosli, predmet, obsah);
} else {
mDbHelper.createNote(username, predmet, obsah, 2);
}
startActivity ();
}
}
public void getData ()
{
EditText predmetInput = ((EditText)findViewById(R.id.predmet));
predmet = predmetInput.getText().toString();
EditText obsahInput = ((EditText)findViewById(R.id.obsah));
obsah = obsahInput.getText().toString();
}
public void startActivity ()
{
PridajPoActivity.this.finish();
}
}
LogCat错误
04-11 17:39:50.670: E/AndroidRuntime(17433): FATAL EXCEPTION: main
04-11 17:39:50.670: E/AndroidRuntime(17433): java.lang.RuntimeException: Unable to start activity ComponentInfo{sk.tomino.login/sk.tomino.login.PridajPoActivity}: java.lang.NullPointerException
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.os.Looper.loop(Looper.java:155)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.ActivityThread.main(ActivityThread.java:5520)
04-11 17:39:50.670: E/AndroidRuntime(17433): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:39:50.670: E/AndroidRuntime(17433): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 17:39:50.670: E/AndroidRuntime(17433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
04-11 17:39:50.670: E/AndroidRuntime(17433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
04-11 17:39:50.670: E/AndroidRuntime(17433): at dalvik.system.NativeStart.main(Native Method)
04-11 17:39:50.670: E/AndroidRuntime(17433): Caused by: java.lang.NullPointerException
04-11 17:39:50.670: E/AndroidRuntime(17433): at sk.tomino.login.PridajPoActivity.onCreate(PridajPoActivity.java:49)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.Activity.performCreate(Activity.java:5066)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
04-11 17:39:50.670: E/AndroidRuntime(17433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
04-11 17:39:50.670: E/AndroidRuntime(17433): ... 11 more
poznamka_pridaj.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff"
android:text="@string/pridat_po" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="@string/predmet" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="text"
android:id="@+id/predmet" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/obsah" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:id="@+id/obsah" />
<Button
android:id="@+id/button_uloz"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClicked_uloz"
android:background="@drawable/button"
android:textColor="#fff"
android:text="@string/button_uloz" />
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
如果odosli
为0长度,则不会设置TextView的句柄。如果声明,你应该在你之外初始化它们。将以下2行移到if语句之外。
tv_nazov = (EditText)findViewById(R.id.meno);
tv_poznamka = (EditText)findViewById(R.id.poznamka);
答案 1 :(得分:0)
试试这个..
EditText
中没有poznamka_pridaj.xml
名称 meno 和 poznamka 。
修改强>
Intent i = getIntent();
if(i.hasExtra(ODOSLI))
odosli = i.getStringExtra(ODOSLI);