所以我一直在研究这个问题,并发现这个问题似乎发生在它无法达到我在我的Activity中引用的内容时。我认为这个问题可能在于我试图达到应用课程,但我不确定。我已经尝试评论与地理编码相关的意图并且仍然得到相同的错误,所以我不确定它在哪里找到空指针。
这是我得到的错误代码:
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 :(得分:1)
在初始化characterCountTextView
之前初始化createTopicEditText
,如下所示
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
方法时,代码updateCharacterCountTextViewText()
尚未初始化
createTopicEditTexts TextChangedListener
答案 1 :(得分:1)
指向不在活动布局中的视图返回NULL
您在ID character_count_textview
中引用的TextView在活动布局activity_create_topic
中不存在。因此发生了NULL POINTER异常。
答案 2 :(得分: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();
}
答案 3 :(得分:1)
由于updateCharacterCountTextViewText ()
方法仅调试,您收到错误,并且createTopicEditText
和maxCharacterCount
都不为空并将characterCountTextView = (TextView) findViewById(R.id.character_count_textview);
移至createTopicEditText.addTextChanged
方法之前< / p>