简体   繁体   English

我的Android应用在启动时崩溃

[英]My Android App Crashes on Launch

I'm trying to build a basic app that displays newstories (currently hardcoded into the Strings.xml file). 我正在尝试构建一个显示新故事的基本应用程序(当前已硬编码到Strings.xml文件中)。 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) 这是我代码的Java部分(我也可以提供其他文件,甚至可以提供我工作区的压缩副本,如果有帮助的话)

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 . 检查您的activity_main.xml并传递button的ID而不是LinearLayout1

also move this below code inside onCreate 也将此代码移到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 将这些内容移到setContentView之后的onCreate中

 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. getResources()是Context的方法,并且Activity创建后即可使用Activity Context。 findViewById looks for a view with the id in the current infalted layout. findViewById在当前infalted布局中查找具有ID的视图。 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 . 代替null使用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. 对同一Activity的setContentView两次以上是一个糟糕的设计。

Also with this textView = new TextView(this); 同样使用此textView = new TextView(this); you have a textview but it is no added to the activity. 您有一个textview,但是没有添加到活动中。

Do read 阅读

http://developer.android.com/training/basics/firstapp/starting-activity.html 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();
            }
        });

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

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