everyone。当我使用Button
和EditView
和ImageView
作为UI时,发生错误,但应用程序可以按我的意愿运行。错误“无法启动活动ComponentInfo”
运行在Debug模型找到:imageView = null
,我在activity_main.XML中定义了imageView
的标识,如image_view
;
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button send_Button;
private EditText editText;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image_view);
editText = (EditText) findViewById(R.id.edit_text);
send_Button = (Button) findViewById(R.id.send_button);
send_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = editText.getText().toString();
Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
}
});
}
}
activity_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type at there."
android:maxLines="2"/>
<Button
android:id="@+id/send_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read_From_Edit"/>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test_photo"/>
</LinearLayout>
我该如何解决这个问题呢?谢谢!
您的活动布局是“activity_main”而不是“activity_view”,它包含视图的 – Raghunandan