2016-05-14 11 views
-1
Process: com.example.sivancohen.rateapp, PID: 2830 
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.sivancohen.rateapp/com.example.sivancohen.rateapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RatingBar.setOnRatingBarChangeListener(android.widget.RatingBar$OnRatingBarChangeListener)' 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.widget.RatingBar.setOnRatingBarChangeListener(android.widget.RatingBar$OnRatingBarChangeListener)' on a null object reference 
at com.example.sivancohen.rateapp.MainActivity.onCreate(MainActivity.java:56) 
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) 
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-14 08:48:37.301 2830-2830/? I/Process: Sending signal. PID: 2830 SIG: 9 

はここに私のアクティビティコードは、私はこのエラーを取得する:java.lang.NullPointerExceptionが:試みは、仮想メソッドを呼び出すために

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    RatingBar ratingBar; 
    Button btn; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent intent = getIntent(); 
     String userName = intent.getStringExtra("userName"); 
     Toast.makeText(MainActivity.this, 
       "Hello, " + userName, Toast.LENGTH_SHORT).show(); 


     ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 
      public void onRatingChanged(RatingBar ratingBar, float rating, 
             boolean fromUser) { 
       Toast.makeText(getApplicationContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show(); 
      } 
     }); 


     btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       float rating = ratingBar.getRating(); 
       Toast.makeText(getApplicationContext(), "Your Selected Ratings : " + 
         "" + String.valueOf(rating), Toast.LENGTH_LONG).show(); 
      } 
     }); 

     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() 
      } 
     }); 
*/ 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     FragmentManager fm = getFragmentManager(); 
     fm.beginTransaction().replace(R.id.content_frame, new electric_fragment()).commit(); 


     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @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; 
    } 

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

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     FragmentManager fm = getFragmentManager(); 
     int id = item.getItemId(); 

     if (id == R.id.nav_electric) { 
      fm.beginTransaction().replace(R.id.content_frame, new electric_fragment()).commit(); 
     } else if (id == R.id.nav_Clothing) { 
      fm.beginTransaction().replace(R.id.content_frame, new clothing_frag()).commit(); 
     } else if (id == R.id.nav_baby) { 
      fm.beginTransaction().replace(R.id.content_frame, new baby_fragment()).commit(); 
     } else if (id == R.id.nav_shoes) { 
      fm.beginTransaction().replace(R.id.content_frame, new shoes_fragment()).commit(); 
     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.sivancohen.rateapp/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.sivancohen.rateapp/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 
} 
+1

http://stackoverflow.com/help/how-リスナーを使用する前に、メソッドを作成する上で、以下の行を書きratingBar.setOnRatingBarChangeListener()を使用して、インスタンスを作成し、XMLとそれを接続し、する必要がありますお問合せ – Shubhank

+0

投稿前に一度あなたの質問を確認してください。決して初期化されていないビューを使用しています。 –

答えて

0

あなたが初期化されていない使用している評価のバーです。あなたは が

ratingbar = findViewById(R.id.yourratingbarid) 
+0

乾草。本当にありがとうございます。原因はありません。原因:java.lang.NullPointerException:nullオブジェクトリファレンスで、仮想メソッド 'void android.widget.RatingBar.setOnRatingBarChangeListener(android.widget.RatingBar $ OnRatingBarChangeListener)'を呼び出そうとしました。 com.example.sivancohen.rateapp.MainActivity.addListenerOnRatingBar(MainActivity.java:78)com.example.sivancohen.rateapp.MainActivity.onCreate(MainActivity.java:44)で、なぜ理解できますか? –

関連する問題