2016-07-11 151 views
0

everyone。當我使用ButtonEditViewImageView作爲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> 

我該如何解決這個問題呢?謝謝!

+4

您的活動佈局是「activity_main」而不是「activity_view」,它包含視圖的 – Raghunandan

回答

0

試圖改變

setContentView(R.layout.activity_view); 

,而不是

setContentView(R.layout.activity_main); 

編輯1

項目 - >清潔

然後再次運行。

+0

哦,不。仍然有問題。但非常感謝你:D –

+0

現在你有什麼錯誤? –

+0

錯誤是:java.lang.RuntimeException:無法啓動活動ComponentInfo。在MainActivity:imageView =(ImageView)findViewById(R.id.image_view)。 –

相關問題