简体   繁体   中英

TextView causing app to crash

For some reason when I call the TextView function it crashes my app. I have looked through the answers of questions similar to mine but I could not narrow down my problem.

This is the first time I am making an app, first time I am using Java for that matter. Previously I only have experience with C++/C.

From my Android Manisfest File

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

From the .xml file:

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

My java file:

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)); is wrong.

You didn't DECLARE a TextView before using it .

Wild guess: you have a NullPointerException, right?
Make sure you have a TextView with id textId in /res/layout/activity_legal_notices.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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