为什么Text = null?

时间:2014-04-22 17:40:47

标签: android null

我是Android和Java的新手,并且在将Text分配给它之后不知道为什么Text值为null?这是一个大学的项目他们没有教我们Java只给了我们Eclipse和Android Sdk和Java需要一些帮助!

package com.example.glossaryapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Syntax extends Activity {

static String Text;
public static TextItem[] Syntax = new TextItem[50];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.syntax);

    final TextView show = (TextView) Syntax.this
            .findViewById(R.id.textViewSyn);

    // Linking the text View widget

    int Count1 = 0;

    // Using the TextView's shows method set text to display the appropriate
    // arrays and indexes

    if (Syntax[0] != null) {

        Text = Syntax[0].displayText();
        show.setText(Text);
    }

    // The Add button for if a user wants to add a new TextItem to the Array
    Button but1 = (Button) findViewById(R.id.butAddSyntax);
    but1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Create.strSender = "Syntax1";

            startActivity(new Intent(Syntax.this, Create.class));

        }// end of on click
    });// butAddSyntax

    // Home Button
    Button but2 = (Button) findViewById(R.id.buttHomeSyntax);
    but2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            finish();

        }// end of on click
    });// buttonHomeSyntax

}

public void onResume() {
    super.onResume();

    TextView show = (TextView) Syntax.this.findViewById(R.id.textViewSyn);

    if (Syntax[0] != null) {

        Text = Syntax[0].displayText().toString();
        show.setText(Text);

    }

}

}

1 个答案:

答案 0 :(得分:1)

使用Android的LogCat将帮助您解决此类问题。

这里有一篇很好的文章:

http://www.vogella.com/tutorials/AndroidLogging/article.html

我认为问题出在这里:

if( Syntax[0] != null ){
    Text = Syntax[0].displayText();
    show.setText(Text);
}

您可以尝试使用LogCat:

if( Syntax[0] != null ){
    Log.d("MyApp", "Sintax (0) = " + Syntax[0].displayText());
    Text = Syntax[0].displayText();
    show.setText(Text);
} else Log.d("MyApp", "Nothing ");

在Logcat窗格中观察结果。