2016-03-01 129 views
0

我開始與Android像2天前,請原諒我,如果我已經犯了一個天真的錯誤。我使用Java而不是XML的接口和應用程序崩潰每次啓動它時該emulator.Here是相同的堆棧跟蹤:應用程序崩潰在Android:運行時異常

03-01 21:08:46.941 7640-7640/com.example.prashant.wisdom E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.prashant.wisdom, PID: 7640 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prashant.wisdom/com.example.prashant.wisdom.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
at com.example.prashant.wisdom.MainActivity.onCreate(MainActivity.java:65) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5417)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
03-01 21:08:49.797 7640-7640/? I/Process: Sending signal. PID: 7640 SIG: 9 

這裏是MainActivity.java:

package com.example.prashant.wisdom; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.RelativeLayout; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    RelativeLayout layout=new RelativeLayout(this); //layout 
    Button button=new Button(this);   //button 
    button.setText("SignIn"); 

    //giving ids to layout and button 

    button.setId(1); 
    layout.setId(2); 

    //rules for button positioning 
    RelativeLayout.LayoutParams buttonDetails=new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT 

    );  //the above snippet is to be written for every widget in the activity. 
      // For example,we'll write one for EditText also. 
    buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); 



    EditText username=new EditText(this);  //username input 

    username.setId(3); 

    RelativeLayout.LayoutParams usernameDetails=new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT 

    );  //rules for the EditText details. 
    //now we've to place the EditText above the button that is already positioned in the center. 
    //the following snippet does that. 

    usernameDetails.addRule(RelativeLayout.ABOVE,button.getId()); 
    usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); //Rule for positioing in the center of the screen 
    usernameDetails.setMargins(0,0,0,50); //padding on all the sides.We're adding padding just to the bottom 
              //hence the rest are zero, but the bottom one is 50 pixels. 
    layout.addView(username);    //EditText now positioned on the layout. 


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    setContentView(layout);      //placement of layout itself 
    layout.addView(button, buttonDetails);  //placement of buttons on the layout 





} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

這裏是content_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.prashant.wisdom.MainActivity" 
tools:showIn="@layout/activity_main"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" /> 
</RelativeLayout> 

回答

0

你沒有一個FAB在XML定義,因此

findViewById(R.id.fab); 

返回null,你會得到一個簡單的NullPointerException異常訪問它

+0

我懷疑是這個問題。如果fab沒有被定義,'R.id.fab'不應該被編譯。如果你看一下谷歌目前的模板,總是有一個帶有工具欄,側邊欄,動作按鈕等的佈局,其中包括內部佈局部分的內容佈局。 (我猜這就是這種情況) – F43nd1r

0

你得叫setContentView之前,你可以找到你的佈局圖(findViewById) 。

此外它似乎沒有任何XML文件實際用於代碼中,因此任何XML中定義的內容都不會被訪問。