我的Android应用程序在启动时崩溃

时间:2014-04-07 09:56:36

标签: java android

我正在尝试构建一个显示newstories的基本应用程序(当前硬编码到Strings.xml文件中)。但即使代码中没有显示错误,它仍会在启动时崩溃。

这是我的代码的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;
        }
    }


});
}

}

3 个答案:

答案 0 :(得分:0)

我认为是因为

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

检查您的activity_main.xml并传递按钮ID,而不是LinearLayout1

还将此代码移到onCreate

中的代码下方
final Button next = (Button) findViewById(R.id.LinearLayout1);
final Resources res = getResources();
final TextView textView = new TextView(null);

答案 1 :(得分:0)

在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()是Context的方法,一旦创建了Activity,就可以使用Activity Context。 findViewById在当前的infalted布局中查找id为id的视图。您需要将布局设置为活动,然后初始化视图。

最后

public TextView (Context context)

但你有

TextView textView = new TextView(null);

而不是null使用this

你也有

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

对于同一个Activity,setContentView的设计不止两次。

同样使用此textView = new TextView(this);您还有一个文本视图,但它没有添加到活动中。

请阅读

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

答案 2 :(得分:0)

尝试这样做。

    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();
            }
        });