TextView不显示日志

时间:2015-11-21 16:44:46

标签: java android

好吧我是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时显示。)

1 个答案:

答案 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方法