Here is my LogCat...
Pls explain it for a noob, this is my first app program... But i need it for my Homework...Pls help me i need to get the echo of a php script and handle this data
05-14 14:03:22.268: E/AndroidRuntime(4674): FATAL EXCEPTION: main
05-14 14:03:22.268: E/AndroidRuntime(4674): java.lang.IllegalStateException: Could not execute method of the activity
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.view.View$1.onClick(View.java:3063)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.view.View.performClick(View.java:3534)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.view.View$PerformClick.run(View.java:14263)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.os.Handler.handleCallback(Handler.java:605)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.os.Looper.loop(Looper.java:137)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 14:03:22.268: E/AndroidRuntime(4674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-14 14:03:22.268: E/AndroidRuntime(4674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-14 14:03:22.268: E/AndroidRuntime(4674): at dalvik.system.NativeStart.main(Native Method)
05-14 14:03:22.268: E/AndroidRuntime(4674): Caused by: java.lang.reflect.InvocationTargetException
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.view.View$1.onClick(View.java:3058)
05-14 14:03:22.268: E/AndroidRuntime(4674): ... 11 more
05-14 14:03:22.268: E/AndroidRuntime(4674): Caused by: android.os.NetworkOnMainThreadException
05-14 14:03:22.268: E/AndroidRuntime(4674): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-14 14:03:22.268: E/AndroidRuntime(4674): at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-14 14:03:22.268: E/AndroidRuntime(4674): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-14 14:03:22.268: E/AndroidRuntime(4674): at com.me.bittwaldstream.MainActivity.sendLogin(MainActivity.java:163)
05-14 14:03:22.268: E/AndroidRuntime(4674): ... 14 more
I dont know what to do... Im confused... This is my first Programm
with Android and Java but i need it for my homework...
(I know it will look like shit :))
Here is my full ActivityMain.java
package com.me.bittwaldstream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.client.ClientProtocolException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new LoginFragment())
.commit();
}
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class LoginFragment extends Fragment {
public LoginFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public static class WaitFragment extends Fragment {
public WaitFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.waiting_main, container, false);
return rootView;
}
}
public static class StreamFragment extends Fragment {
public StreamFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.videostream_main, container, false);
return rootView;
}
}
private static String readStream(InputStream is) {
final String TAG = "Stream";
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
Log.e(TAG, "IOException", e);
} finally {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
}
return sb.toString();
}
//App beenden
public void abortApp(View v) {
finish();
}
//Login prüfen
public void sendLogin(View v) {
/*getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new WaitFragment())
.commit();*/
EditText username;
EditText passwort;
String result = "";
username = (EditText) findViewById(R.id.editTextUsername);
passwort = (EditText) findViewById(R.id.editTextPasswort);
if(username.getText().toString().trim().length() <= 0)
{
Toast.makeText(this, "Bitte geben Sie einen Usernamen an", Toast.LENGTH_LONG).show();
}
if(passwort.getText().toString().trim().length() <= 0)
{
Toast.makeText(this, "Bitte geben Sie ihr Passwort an", Toast.LENGTH_LONG).show();
}
URL url;
try {
url = new URL("http://shop.kokakukidotai.cf/echotest.php");
HttpURLConnection mUrlConnection = (HttpURLConnection) url.openConnection();
mUrlConnection.setDoInput(true);
InputStream is = new BufferedInputStream(mUrlConnection.getInputStream());
result = readStream(is);
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
}
if(result == "allow")
{
Toast.makeText(this, "Login erfolgreich", Toast.LENGTH_LONG).show();
}
}
public void acceptRequest(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new StreamFragment())
.commit();
}
public void overjump(View v) {
Button AcceptEvent;
ProgressBar Wait;
TextView Message;
AcceptEvent = (Button) findViewById(R.id.buttonAccept);
Wait = (ProgressBar) findViewById(R.id.progressBarWait);
Message = (TextView) findViewById(R.id.textViewWaiting);
Wait.setVisibility(View.GONE);
AcceptEvent.setVisibility(View.VISIBLE);
Message.setText(R.string.accept_event);
}
}
Here are my Mainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.bittwaldstream"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<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"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.me.bittwaldstream.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You are throwing a NetworkOnMainThreadException by performing network operations on your main Activity.
This is in place since Ice Cream Sandwitch.
Use an AsyncTask, Service, Handler, or in general, a background Thread to perform those operations.
Related
I am trying to create an Image Slider containing 4 images for my application and i've been running into the following errors. I've tried searching and have found similiar titles but the problems are different.
This is the logcat:
example.prgguru.com.wallit E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.prgguru.com.wallit, PID: 32048
java.lang.RuntimeException: Unable to start activity ComponentInfo{example.prgguru.com.wallit/example.prgguru.com.wallit.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class example.prgguru.com.wallit.MainActivityFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:585)
at android.app.Fragment.instantiate(Fragment.java:561)
at android.app.Activity.onCreateView(Activity.java:5066)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:585)
at android.app.Fragment.instantiate(Fragment.java:561)
at android.app.Activity.onCreateView(Activity.java:5066)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
This is my Main Activity.java:
package example.prgguru.com.wallit;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:name="example.prgguru.com.wallit.MainActivityFragment"
android:id="#+id/fragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32383d">
<!-- ViewPager -->
<!-- Footer -->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageviewPager" />
<include layout="#layout/footer"/>
</FrameLayout>
MainActivityFragment.java
package example.prgguru.com.wallit;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivityFragment extends Fragment {
private ViewPager _mViewPager;
private ImageViewPagerAdapter _adapter;
private ImageView _btn1, _btn2, _btn3,_btn4;
public MainActivityFragment() {
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpView();
setTab();
onCircleButtonClick();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
private void onCircleButtonClick() {
_btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn1.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(0);
}
});
_btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn2.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(1);
}
});
_btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn3.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(2);
}
});
_btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn4.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(3);
}
});
}
private void setUpView() {
_mViewPager = (ViewPager) getView().findViewById(R.id.imageviewPager);
_adapter = new ImageViewPagerAdapter(getActivity(), getFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
initButton();
}
private void setTab() {
_mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int position) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
_btn1.setImageResource(R.drawable.holo_circle);
_btn2.setImageResource(R.drawable.holo_circle);
_btn3.setImageResource(R.drawable.holo_circle);
_btn4.setImageResource(R.drawable.holo_circle);
btnAction(position);
}
});
}
private void btnAction(int action) {
switch (action) {
case 0:
_btn1.setImageResource(R.drawable.fill_circle);
break;
case 1:
_btn2.setImageResource(R.drawable.fill_circle);
break;
case 2:
_btn3.setImageResource(R.drawable.fill_circle);
break;
case 3:
_btn4.setImageResource(R.drawable.fill_circle);
break;
}
}
private void initButton() {
_btn1 = (ImageView) getView().findViewById(R.id.btn1);
_btn1.setImageResource(R.drawable.fill_circle);
_btn2 = (ImageView) getView().findViewById(R.id.btn2);
_btn3 = (ImageView) getView().findViewById(R.id.btn3);
_btn4 = (ImageView) getView().findViewById(R.id.btn4);
}
private void setButton(Button btn, String text, int h, int w) {
btn.setWidth(w);
btn.setHeight(h);
btn.setText(text);
}
}
Your Activity extends from Activity and not AppCompatActivity. But your fragment extends from android.support.v4.app.Fragment. You must use one type of fragment: from support library or normal one.
Try to change extends Fragment from android.app.Fragment in your fragment class.
I am trying to display an array of strings in a list view. the app crashes saying null pointer exception but it does not specify the line in which null pointer occurs.
ReportHistoryFragment:-
package com.appdev.ngoapp;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import java.util.Iterator;
import java.util.List;
/**
* Created by Adarsh on 01-02-2015.
*/
public class ReportHistoryFragment extends Fragment {
View reportHistory;
ParseUser parseUser;
ParseQuery<ParseObject> parseQuery,pq;
ParseObject parseObject;
String[] title;
Context context;
ArrayAdapter<String> stringArrayAdapter;
ListView listView;
int size,i=0;
ProgressDialog progressDialog;
Iterator<ParseObject> iterator;
public ReportHistoryFragment(Context context) {
this.context=context;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
reportHistory = inflater.inflate(R.layout.report_history_layout,container,false);
listView = (ListView) reportHistory.findViewById(R.id.list_history);
return reportHistory;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parseUser = ParseUser.getCurrentUser();
progressDialog = ProgressDialog.show(context,"Downloading Data","Please wait...",true);
progressDialog.setCancelable(false);
downloadData();
}
public void downloadData() {
parseQuery = ParseQuery.getQuery("Reports");
parseQuery.whereEqualTo("user",parseUser);
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
iterator = parseObjects.iterator();
size = parseObjects.size();
title = new String[size];
i=0;
while (iterator.hasNext()) {
parseObject = iterator.next();
pq = ParseQuery.getQuery("Reports");
Log.v("ID",parseObject.getObjectId());
pq.getInBackground(parseObject.getObjectId(), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
title[i] = object.getString("title");
i++;
} else {
e.printStackTrace();
}
if(i==size-1) {
stringArrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,title);
listView.setAdapter(stringArrayAdapter);
}
}
});
}
} else {
e.printStackTrace();
}
}
});
}
}
report_history_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list_history">
</ListView>
</RelativeLayout>
Logcat
02-03 20:34:07.780 18717-18717/com.appdev.ngoapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.appdev.ngoapp, PID: 18717
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16521)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16521)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16521)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:16521)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16521)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16521)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1450)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16521)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16521)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1450)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16521)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
at android.view.View.measure(View.java:16521)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Check if you're actually putting anything in your title array. Then check whether stringArrayAdapter is initializing properly.
I got problem with my first Android application. Just after running, my app crashes in emulator with "Unfortunately has stopped." message. I checked many threads on Stack and I read pretty much of official documentation and I really do not know what's a cause...
I use AVD with Android 4.4.2, API Level 19. CPU/ABI - ARM. Use Host GPU - checked (without this one emulator works very slowly).
My app should play a song after pressing the button. I would be really grateful for any ideas. Cheers.
Java file:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
import android.media.MediaPlayer;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Button one = (Button) findViewById(R.id.button1);
final MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.elements6);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.start();
}
});
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
And XML file:
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Errors:
05-14 17:29:21.740: D/AndroidRuntime(1145): Shutting down VM
05-14 17:29:21.740: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0xb2abcba8)
05-14 17:29:21.750: E/AndroidRuntime(1145): FATAL EXCEPTION: main
05-14 17:29:21.750: E/AndroidRuntime(1145): Process: com.example.beatzlooper, PID: 1145
05-14 17:29:21.750: E/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beatzlooper/com.example.beatzlooper.MainActivity}: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:136)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-14 17:29:21.750: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): Caused by: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.example.beatzlooper.MainActivity.onCreate(MainActivity.java:31)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Activity.performCreate(Activity.java:5231)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-14 17:29:21.750: E/AndroidRuntime(1145): ... 11 more
You are working with fragments.
The Fragment cannot be added to the R.id.container View because it doesn't exists.
Just delete the whole if-clause in the onCreate() and also delete the Placeholder Class you don't need it actually.
Comment this code
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/
When i run app it give message unfortunately loca has stopped and close app
Java coding
package com.example.loca;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat, place1, currentlocation, btn_text;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Toast.makeText(getApplicationContext(), currentlocation , Toast.LENGTH_LONG).show();
}
#Override
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), currentlocation , Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
AND LOGCAT is
05-14 01:20:39.210: D/AndroidRuntime(1177): Shutting down VM
05-14 01:20:39.210: W/dalvikvm(1177): threadid=1: thread exiting with uncaught exception (group=0xb3aebb90)
05-14 01:20:39.230: E/AndroidRuntime(1177): FATAL EXCEPTION: main
05-14 01:20:39.230: E/AndroidRuntime(1177): Process: com.example.loca, PID: 1177
05-14 01:20:39.230: E/AndroidRuntime(1177): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loca/com.example.loca.MainActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Looper.loop(Looper.java:137)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-14 01:20:39.230: E/AndroidRuntime(1177): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 01:20:39.230: E/AndroidRuntime(1177): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 01:20:39.230: E/AndroidRuntime(1177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-14 01:20:39.230: E/AndroidRuntime(1177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-14 01:20:39.230: E/AndroidRuntime(1177): at dalvik.system.NativeStart.main(Native Method)
05-14 01:20:39.230: E/AndroidRuntime(1177): Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Parcel.readException(Parcel.java:1461)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Parcel.readException(Parcel.java:1415)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:540)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:860)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:454)
05-14 01:20:39.230: E/AndroidRuntime(1177): at com.example.loca.MainActivity.onCreate(MainActivity.java:34)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.Activity.performCreate(Activity.java:5243)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-14 01:20:39.230: E/AndroidRuntime(1177): ... 11 more
05-14 01:20:47.040: I/Process(1177): Sending signal. PID: 1177 SIG: 9
You need to request the FINE_LOCATION permission, just like the exception says.
Your logcat clearly said
Caused by: java.lang.SecurityException: "gps" location provider
requires ACCESS_FINE_LOCATION permission. 05-14 01:20:39.230:
E/AndroidRuntime(1177):
You need to add below permission in your manifest.xml file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
The log says it all :
location provider requires ACCESS_FINE_LOCATION permission.
Add this permission in AndroidManifest.xml and your app will work fine.
I try to change 3 textviews in a class called UserProfile() calling the method update() from the class UpdateProfile(), the class UserProfile do something like this:
package com.safm;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class UserProfile extends Activity {
TextView username, usersurname, useremail;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
username = (TextView) findViewById(R.id.username);
usersurname = (TextView) findViewById(R.id.usersurname);
useremail = (TextView) findViewById(R.id.useremail);
}
public void updateButton(View view){
Intent i = new Intent(this, UpdateProfile.class);
startActivity(i);
}
public void update(String nname, String nusername, String nemail){
System.out.println("2");
System.out.println(nname);
username.setText(nname);
usersurname.setText(nusername);
useremail.setText(nemail);
System.out.println("3");
}
}
The updateButton method invoke the UpdateProfile class:
package com.safm;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateProfile extends Activity {
EditText newusernametxt, newsurnametxt, newemailtxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actualizarperfil);
newusernametxt = (EditText) findViewById(R.id.newusernametxt);
newsurnametxt = (EditText) findViewById(R.id.newsurnametxt );
newemailtxt = (EditText) findViewById(R.id.newemailtxt );
}
public void updateInfo(View view){
String nname = newusernametxt .getText().toString();
String nsurname = newsurnametxt .getText().toString();
String nemail = newemailtxt .getText().toString();
if(nname.compareTo("") != 0 && nsurname.compareTo("") != 0 && nemail.compareTo("") != 0){
UserProfile profile = new UserProfile();
System.out.println("1");
profile.update(nname, nsurname, nemail);
System.out.println("4");
Toast.makeText(this, "Success, the profile has been updated", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "You can't empty fields", Toast.LENGTH_SHORT).show();
}
}
This is my logcat file, as you can see, the first 2 flags are printed correctly, the error is when I try to set another text on the TextView, but the value of the strings (nname, nsurname and nemail) are not empty cause also is printed in the console HELP!
05-14 21:43:31.140: I/System.out(878): 1
05-14 21:43:31.140: I/System.out(878): 2
05-14 21:43:31.150: I/System.out(878): TextEditValue
05-14 21:43:31.150: D/AndroidRuntime(878): Shutting down VM
05-14 21:43:31.150: W/dalvikvm(878): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-14 21:43:31.310: E/AndroidRuntime(878): FATAL EXCEPTION: main
05-14 21:43:31.310: E/AndroidRuntime(878): java.lang.IllegalStateException: Could not execute method of the activity
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View$1.onClick(View.java:3599)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View.performClick(View.java:4204)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View$PerformClick.run(View.java:17355)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.os.Handler.handleCallback(Handler.java:725)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:137)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 21:43:31.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 21:43:31.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 21:43:31.310: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)
05-14 21:43:31.310: E/AndroidRuntime(878): Caused by: java.lang.reflect.InvocationTargetException
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View$1.onClick(View.java:3594)
05-14 21:43:31.310: E/AndroidRuntime(878): ... 11 more
05-14 21:43:31.310: E/AndroidRuntime(878): Caused by: java.lang.NullPointerException
05-14 21:43:31.310: E/AndroidRuntime(878): at com.safm.UserProfile.update(UserProfile.java:31)
05-14 21:43:31.310: E/AndroidRuntime(878): at com.safm.UserProfile.updateInfo(UpdateProfile.java:33)
05-14 21:43:31.310: E/AndroidRuntime(878): ... 14 more
NOTE: I've clean the project and defined the TextView in ProfileUser class and the TextEdit in UpdateProfile outside the onCreate method but doesn't work.
Thanks.
Activities in Android are separate containers, that are in no way allowed to directly touch eachother. This is because the application stack behind Android 'freezes' the activities when they are no longer on the foreground, and could even be swapped to hard storage to save on RAM at the operating system's discretion.
Your UserProfile encapsulation should just reload its data after the UpdateProfile activity is closed, or request communication back the proper way via startActivityForResult.
Your current implementation is technically incorrect from each perspective, since you're hardcoding the UpdateProfile activity to always be called from a UserProfile activity. What if some day you introduce a new menu option to call it directly from the home screen? Activities are always separate, and should not make such assumptions.