2015-05-17 46 views
0

所以,我一直在研究這個問題,並發現這個問題似乎發生時,它不能達到的東西,我引用在我的活動。我認爲這個問題可能在於我試圖達到課程申請,但我不確定。我試着評論與地理編碼相關的意圖,但仍然得到相同的錯誤,所以我不確定它在哪裏找到空指針。錯誤:「無法啓動活動ComponentInfo ...顯示java.lang.NullPointerException」

這是錯誤代碼我得到:

05-17 01:50:11.583: E/AndroidRuntime(23183): FATAL EXCEPTION: main 
05-17 01:50:11.583: E/AndroidRuntime(23183): Process: com.binarsunset.topic, PID: 23183 
05-17 01:50:11.583: E/AndroidRuntime(23183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.binarsunset.topic/com.binarsunset.topic.CreateTopicActivity}: java.lang.NullPointerException 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.os.Handler.dispatchMessage(Handler.java:102) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.os.Looper.loop(Looper.java:136) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.ActivityThread.main(ActivityThread.java:5097) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at java.lang.reflect.Method.invokeNative(Native Method) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at java.lang.reflect.Method.invoke(Method.java:515) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at dalvik.system.NativeStart.main(Native Method) 
05-17 01:50:11.583: E/AndroidRuntime(23183): Caused by: java.lang.NullPointerException 
05-17 01:50:11.583: E/AndroidRuntime(23183): at com.binarsunset.topic.CreateTopicActivity.updateCharacterCountTextViewText(CreateTopicActivity.java:118) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at com.binarsunset.topic.CreateTopicActivity.onCreate(CreateTopicActivity.java:71) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.Activity.performCreate(Activity.java:5248) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
05-17 01:50:11.583: E/AndroidRuntime(23183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 
05-17 01:50:11.583: E/AndroidRuntime(23183): ... 11 more 

所以我在活動開始TopicActivity但它不是我的主要,這裏是該代碼:

package com.binarsunset.topic; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

/** 
* Created by Douglas on 5/3/2015. 
*/ 
public class TopicActivity extends ActionBarActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_topic); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
      case R.id.Create: { 
       aboutItemCreate(); 
       return true; 
      } 


      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    private void aboutItemCreate(){ 
     Intent intent = new Intent(getApplicationContext(), CreateTopicActivity.class); 
     startActivity(intent); 
    } 


} 

我點擊在操作欄上創建活動,這會將我帶到CreateTopicActivity,並且此處它崩潰的位置是CreateTopicAcvivity的代碼:

package com.binarsunset.topic; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.location.Location; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.parse.ParseACL; 
import com.parse.ParseException; 
import com.parse.ParseGeoPoint; 
import com.parse.ParseUser; 
import com.parse.SaveCallback; 

/** 
* Created by Douglas on 5/16/2015. 
*/ 
public class CreateTopicActivity extends Activity { 

    // UI references. 
    private EditText createTopicEditText; 
    private TextView characterCountTextView; 
    private Button createButton; 

    private int maxCharacterCount = Application.getConfigHelper().getPostMaxCharacterCount(); 
    private ParseGeoPoint geoPoint; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_create_topic); 


     Intent intent = getIntent(); 
     Location location = intent.getParcelableExtra(Application.INTENT_EXTRA_LOCATION); 
     geoPoint = new ParseGeoPoint(location.getLatitude(), location.getLongitude()); 

     createTopicEditText = (EditText) findViewById(R.id.create_topic_title); 
     createTopicEditText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      } 

      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       updateTopicButtonState(); 
       updateCharacterCountTextViewText(); 
      } 
     }); 

     characterCountTextView = (TextView) findViewById(R.id.character_count_textview); 

     createButton = (Button) findViewById(R.id.create_topic_button); 
     createButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       createTopic(); 
      } 
     }); 

     updateTopicButtonState(); 
     updateCharacterCountTextViewText(); 
    } 

    private void createTopic() { 
     String text = createTopicEditText.getText().toString().trim(); 

     // Set up a progress dialog 
     final ProgressDialog dialog = new ProgressDialog(CreateTopicActivity.this); 
     dialog.setMessage(getString(R.string.progress_topic)); 
     dialog.show(); 

     // Create a post. 
     AnywallPost topic = new AnywallPost(); 

     // Set the location to the current user's location 
     topic.setLocation(geoPoint); 
     topic.setText(text); 
     topic.setUser(ParseUser.getCurrentUser()); 
     topic.setInt(1); 
     ParseACL acl = new ParseACL(); 

     // Give public read access 
     acl.setPublicReadAccess(true); 
     topic.setACL(acl); 

     // Save the topic 
     topic.saveInBackground(new SaveCallback() { 
      @Override 
      public void done(ParseException e) { 
       dialog.dismiss(); 
       finish(); 
      } 
     }); 
    } 

    private String getTopicEditTextText() { 
     return createTopicEditText.getText().toString().trim(); 
    } 

    private void updateTopicButtonState() { 
     int length = getTopicEditTextText().length(); 
     boolean enabled = length > 0 && length < maxCharacterCount; 
     createButton.setEnabled(enabled); 
    } 

    private void updateCharacterCountTextViewText() { 
     String characterCountString = String.format("%d/%d", createTopicEditText.length(), maxCharacterCount); 
     characterCountTextView.setText(characterCountString); 
    } 

} 

這裏是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.binarsunset.topic" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="19" /> 

    <permission 
     android:name="com.binarsunset.Topic.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" > 
    </permission> 

    <uses-permission android:name="com.parse.anywall.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <!-- 
    The following permission is not required to use 
    Google Maps Android API v2, but is recommended. 
    --> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <application 
     android:name=".Application" 
     android:allowBackup="true" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" /> 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyCVz0h90fBZwkcgagi1Q7WV1W_cVgd0_QA" /> 

     <meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".LoginActivity" 
      android:label="@string/title_activity_login" 
      android:screenOrientation="portrait" /> 

     <activity 
      android:name=".SignUpActivity" 
      android:label="@string/title_activity_signup" 
      android:screenOrientation="portrait" /> 

     <activity 
      android:name=".DispatchActivity" 
      android:noHistory="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".WelcomeActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" 
      android:theme="@android:style/Theme.Holo" /> 
     <activity 
      android:name=".PostActivity" 
      android:label="@string/activity_post_title" 
      android:windowSoftInputMode="adjustResize" /> 
     <activity 
      android:name=".TopicActivity" 
      android:label="Topic" /> 
     <activity 
      android:name=".PostCommentsActivity" 
      android:label="PostComments" /> 
     <activity 
      android:name=".SettingsActivity" 
      android:label="@string/title_activity_settings" /> 
     <activity 
      android:name=".LocationActivity" 
      android:label="Location" /> 

     <activity 
      android:name=".CreateTopicActivity" 
      android:label="CreateTopic" /> 

    </application> 

</manifest> 

這裏是我的activity_create_topic.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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" 
    tools:context=".PostActivity" 
> 

    <EditText 
     android:id="@+id/create_topic_title" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:background="@null" 
     android:hint="@string/create_topic_title" 
     android:inputType="text" 
     android:gravity="top" 
     android:layout_above="@+id/create_topic_details"> 
     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/create_topic_details" 
     android:layout_width="fill_parent" 
     android:layout_height="300dp" 
     android:layout_above="@+id/character_count_Create_Details" 
     android:background="@null" 
     android:hint="@string/create_topic_details" 
     android:inputType="textMultiLine" 
     android:gravity="top" > 
     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/create_topic_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" 
     android:text="@string/Create_text" /> 

    <TextView 
     android:id="@+id/character_count_Create_Details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginBottom="@dimen/activity_vertical_margin" 
     android:layout_above="@id/create_topic_button" 
     android:text="@string/character_count_placeholder" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 


</RelativeLayout> 

讓我知道如果你需要任何額外的代碼。感謝您的時間。

編輯:另外我註釋掉了一切,但佈局創造者在我CreateTopicActivity和它跑不崩潰如此看來問題就出在CreateTopicActivity的代碼。這導致我相信它的地理編碼或應用程序的引用,但即使我知道這是我仍然是新的錯誤,所以我真的不知道我會如何解決這個問題或爲什麼這是一個問題第一個地方。

+0

,的CreateTopicActivity。 – theapache64

回答

1

調用updateCharacterCountTextViewText()方法指着查看初始化初始化您createTopicEditText以前這是不是在活動佈局返回NULL

你通過ID character_count_textview指TextView的是不活動的佈局activity_create_topic存在。所以發生了NULL POINTER異常。

+0

它工作嗎? – theapache64

+0

仍然同樣的錯誤悲傷:( – gunR

+0

@gunR POST您的'activity_create_topic.xml'佈局。我認爲您使用'character_count_textview'指向的TextView不在該佈局中。 – theapache64

1

初始化characterCountTextView如下圖所示

characterCountTextView = (TextView) findViewById(R.id.character_count_textview); 
    createTopicEditText = (EditText) findViewById(R.id.create_topic_title); 
    createTopicEditText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      updateTopicButtonState(); 
      updateCharacterCountTextViewText(); 
     } 
    }); 

請注意,在你的代碼characterCountTextView尚未當你從你的 createTopicEditTexts TextChangedListener

+0

嗯,我添加了UI聲明和onCreate聲明,但我仍然得到相同的錯誤。 – gunR

1

我覺得你maxCharacterCount爲空。

嘗試下面的代碼行移動到的onCreate。

maxCharacterCount = Application.getConfigHelper().getPostMaxCharacterCount(); 

像下面

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_topic); 

    maxCharacterCount = Application.getConfigHelper().getPostMaxCharacterCount(); 
    Intent intent = getIntent(); 
    Location location = intent.getParcelableExtra(Application.INTENT_EXTRA_LOCATION); 
    geoPoint = new ParseGeoPoint(location.getLatitude(), location.getLongitude()); 
    characterCountTextView = (TextView) findViewById(R.id.character_count_textview); 
    createTopicEditText = (EditText) findViewById(R.id.create_topic_title); 
    createTopicEditText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      updateTopicButtonState(); 
      updateCharacterCountTextViewText(); 
     } 
    }); 

    createButton = (Button) findViewById(R.id.create_topic_button); 
    createButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      createTopic(); 
     } 
    }); 

    updateTopicButtonState(); 
    updateCharacterCountTextViewText(); 
} 
1

你得到的錯誤,因爲updateCharacterCountTextViewText()方法只是調試和看到兩個createTopicEditTextmaxCharacterCount是NOT NULL和移動characterCountTextView = (TextView) findViewById(R.id.character_count_textview);馬克行號118之前createTopicEditText.addTextChanged方法

相關問題