TextView导致应用崩溃

时间:2014-05-30 16:07:05

标签: java android eclipse

出于某种原因,当我调用TextView函数时,它会崩溃我的应用程序。 我已经查看了类似于我的问题的答案,但我无法缩小我的问题范围。

这是我第一次制作应用,第一次使用Java就是这个问题。以前我只有C ++ / C的经验。

来自我的Android Manisfest文件

<activity
        android:name="sg.blogspot.ce9005project.LegalNoticesActivity"
        android:label="@string/action_legalnotices" >
</activity>

来自.xml文件:

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textId"/>

我的java文件:

package sg.blogspot.ce9005project;

import com.google.android.gms.common.GooglePlayServicesUtil;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class LegalNoticesActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_legal_notices);

        //Original line below
        ((TextView)                  
findViewById(R.id.textId)).setText(GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this));

    //The 2 lines below were modified by me but still do not work
    //TextView textView = (TextView) findViewById(R.id.textId);
    //textView.setText("TEST");     
    //The below line works. The problem, that means, lies with TextView
    //Toast.makeText(this, GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this),      
Toast.LENGTH_SHORT).show();
}
}

1 个答案:

答案 0 :(得分:1)

findViewById(R.id.textId)).setText(GooglePlayServicesUtil.getOpenSourceSoftware‌​LicenseInfo(this));错了。

在使用TextView之前,您没有 DECLARE

狂野猜测:你有一个NullPointerException,对吗? 确保在textId

中有一个ID为/res/layout/activity_legal_notices.xml的TextView