好吧我是android的新手,我一直在学习一门课程,但有些事我不明白。
这是LifeCycleT
活动。
package com.example.killtheviolence.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LifeCycleT extends Activity {
StringBuilder builde = new StringBuilder();
TextView textview;
private void log(String text) {
Log.d("LifeCycleT", text);
builde.append( builde.toString() );
builde.append("\n");
textview.setText(builde.toString());
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText(builde.toString());
setContentView(textview);
}
protected void onResume() {
super.onResume();
log("resume");
}
protected void onPause() {
super.onPause();
log("paused");
if(isFinishing()) {
log("death");
}
}
}
这是从哪里来的?来自ListView
。
package com.example.killtheviolence.myapplication;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
String prueva[] = {"LifeCycleT","item2","item3","item4",
"item5 ", "item6 "};
public void onCreate(Bundle savedInstanceSate){
super.onCreate(savedInstanceSate);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, prueva));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String gate = prueva[position];
try {
Class<?> inst;
inst = Class.forName("com.example.killtheviolence.myapplication." + gate);
Intent intent = new Intent(this, inst);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
这是主要活动。
我要执行的操作会在屏幕上的TextView
Log.d
中显示,但我收到错误消息:
无法恢复活动 {com.example.killtheviolence.myapplication / com.example.killtheviolence.myapplication.LifeCycleT}: 显示java.lang.NullPointerException
所以我找了一个理由(意思是NullPointerException
就是这样
在初始化TextView
行之前,我使用了textview.setText(builde.toString());
我所做的是在TextView
函数中创建log
的实例。
private void log(String text) {
Log.d("LifeCycleT", text);
builde.append(builde.toString());
builde.append("\n");
TextView textview = new TextView(this);
textview.setText(builde.toString());
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText(builde.toString());
setContentView(textview);
}
为什么要创建TextView
的两个实例?为什么我要setText
两次?为什么它在TextView
中没有显示任何内容? (仅在我在"string"
的参数中写setText
时显示。)
答案 0 :(得分:0)
修复您的onCreate
以设置字段参数而不是本地参数:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
textview = new TextView(this); // << This used to be a local var, thats why NPE
textview.setText(builde.toString());
setContentView(textview);
}
然后从TextView
log
的第二次创建
private void log(String text) {
Log.d("LifeCycleT", text);
builde.append("\n");
builde.append(text);
textview.setText(builde.toString());
}
另请注意,我更改了您的StringBuilder
,将您传递的文字作为参数附加到log
方法