简体   繁体   中英

My Android App Crashes on Launch

I'm trying to build a basic app that displays newstories (currently hardcoded into the Strings.xml file). But it keeps crashing on launch, even though there are no errors being shown in the code.

Here's the Java part of my code (I can also supply any other files, or even a zipped copy of my workspace if that'll help anyone)

package cara.app;

import android.os.Build;  
import android.os.Bundle;  
import android.annotation.SuppressLint;  
import android.app.Activity;  
import android.content.res.Resources;  
import android.view.Menu;  
import android.view.View;  
import android.widget.Button;  
import android.widget.TextView;  


public class MainActivity extends Activity {

    final Resources res = getResources();
    final TextView textView = new TextView(null);
    final Button next = (Button) findViewById(R.id.LinearLayout1);

    @SuppressLint("NewApi")
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.LinearLayout1);
        button.setText("" + res.getString(R.string.News) + "\n\n" + res.getString(R.string.Story_Title_1) + "\n\n" + res.getString(R.string.Story_Title_2) + "\n\n" + res.getString(R.string.Story_Title_3) + "\n\n" + res.getString(R.string.Story_Title_4) + "\n\n" + res.getString(R.string.Story_Title_5));

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {                   
                OpenNews();
            }
        });

        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }

}
public void selfDestruct(View view) {
    // Boom 
}


@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void OpenNews()
{

    // Create the text view
    textView.setTextSize(40);
    textView.setText(res.getString(R.string.Story_1));

    // Set the text view as the activity layout
    setContentView(textView);

    next.setText("Next");

    next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int itemNumber = 1;

        if(itemNumber == 1){
            textView.setText(res.getString(R.string.Story_2));
            itemNumber = 2;
        }
        else if(itemNumber == 2){
            textView.setText(res.getString(R.string.Story_3));
            itemNumber = 3;
        }
        else if(itemNumber == 3){
            textView.setText(res.getString(R.string.Story_4));
            itemNumber = 4;
        }
        else if(itemNumber == 4){
            textView.setText(res.getString(R.string.Story_5));
            itemNumber = 5;
        }
        else if(itemNumber == 5){
            textView.setText(res.getString(R.string.Story_1));
            itemNumber = 1;
        }
    }


});
}

}

I think it's because

final Button button = (Button) findViewById(R.id.LinearLayout1);

Check your activity_main.xml and pass the ID of button instead of LinearLayout1 .

also move this below code inside onCreate

final Button next = (Button) findViewById(R.id.LinearLayout1);
final Resources res = getResources();
final TextView textView = new TextView(null);

Move these inside onCreate after setContentView

 Resources res;
 TextView textView;
 Button next; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 res = getResources(); 
 textView = new TextView(this); // use Activity COntext
 next = (Button) findViewById(R.id.LinearLayout1);

getResources() is method of Context and Activity Context is available once Activity is created. findViewById looks for a view with the id in the current infalted layout. You need to set the layout to the activity and then initialize views.

Lastly

public TextView (Context context)

But you had

TextView textView = new TextView(null);

Instead of null use this .

Also you have

  setContentView(R.layout.activity_main);
  setContentView(textView);

Its a bad design to have setContentView more than twice for the same Activity.

Also with this textView = new TextView(this); you have a textview but it is no added to the activity.

Do read

http://developer.android.com/training/basics/firstapp/starting-activity.html

Try doing it in that way.

    private Button next = null;

    @SuppressLint("NewApi")
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next  = (Button) findViewById(R.id.LinearLayout1);
        final Button button = (Button) findViewById(R.id.LinearLayout1);
        button.setText("" + res.getString(R.string.News) + "\n\n" + res.getString(R.string.Story_Title_1) + "\n\n" + res.getString(R.string.Story_Title_2) + "\n\n" + res.getString(R.string.Story_Title_3) + "\n\n" + res.getString(R.string.Story_Title_4) + "\n\n" + res.getString(R.string.Story_Title_5));

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {                   
                OpenNews();
            }
        });

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