Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的Android应用程序在发布时崩溃_Java_Android - Fatal编程技术网

Java 我的Android应用程序在发布时崩溃

Java 我的Android应用程序在发布时崩溃,java,android,Java,Android,我正在尝试构建一个基本的应用程序来显示新闻故事(目前硬编码到Strings.xml文件中)。但它在启动时不断崩溃,即使代码中没有显示错误 这是我代码的Java部分(我还可以提供任何其他文件,甚至是我工作区的压缩副本,如果这对任何人都有帮助的话) }我想是因为 final Button button = (Button) findViewById(R.id.LinearLayout1); 检查您的活动\u main.xml并传递按钮的ID,而不是线性布局1 同时将此代码移到onCreate中的下

我正在尝试构建一个基本的应用程序来显示新闻故事(目前硬编码到Strings.xml文件中)。但它在启动时不断崩溃,即使代码中没有显示错误

这是我代码的Java部分(我还可以提供任何其他文件,甚至是我工作区的压缩副本,如果这对任何人都有帮助的话)

}我想是因为

final Button button = (Button) findViewById(R.id.LinearLayout1);
检查您的
活动\u main.xml
并传递按钮的ID,而不是
线性布局1

同时将此代码移到onCreate中的下面

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

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

最后

public TextView (Context context)
但是你有

TextView textView = new TextView(null);
使用
代替null

你也有

  setContentView(R.layout.activity_main);
  setContentView(textView);
对于同一个活动,setContentView超过两次是一个糟糕的设计

也使用此
textView=新的textView(此)您有一个文本视图,但未将其添加到活动中


试着用那种方式做

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

移动
next=(按钮)findViewById(R.id.LinearLayout1)
setContentView
之后创建活动,并使用
textView=newtextView(MainActivity.this)而不是
textView=newtextview(空)
setContentView
之后,logcat的错误是什么?把它贴在这里。确定R.id.LinearLayout1是一个按钮吗?
    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();
            }
        });