繁体   English   中英

TextView导致应用崩溃

[英]TextView causing app to crash

由于某种原因,当我调用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();
}
}

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

在使用TextView之前,您没有对它进行过声明

疯狂猜测:您有NullPointerException,对吗?
确保在/res/layout/activity_legal_notices.xml具有ID为textId的TextView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM