Unfortunately the Wifi Scanner has stopped [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I was trying to make a wifi scanner which can show the list of all the wifi access points. My code is as following :
MainActivity.java :
package com.example.wifiscanner;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
WifiManager wifi;
WifiScanReceiver wifireciever;
Button scan;
List<ScanResult> wifilist;
ListView list;
String wifis[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
list=(ListView)findViewById(R.id.listView1);
scan=(Button)findViewById(R.id.button1);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==false){
wifi.setWifiEnabled(true);
}
wifireciever = new WifiScanReceiver();
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
}
});
}
protected void onPause() {
unregisterReceiver(wifireciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
#SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifilist = wifi.getScanResults();
wifis = new String[wifilist.size()];
for(int i = 0; i < wifilist.size(); i++){
wifis[i] = ((wifilist.get(i)).toString());
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
}
#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;
}
}
fragment_main.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="com.example.wifiscanner.MainActivity$PlaceholderFragment" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true" >
</ListView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/listView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="37dp"
android:minWidth="#dimen/abc_action_bar_stacked_tab_max_width"
android:text="Scan" />
</RelativeLayout>
Log cat :
05-26 09:41:49.840: D/AndroidRuntime(11186): Shutting down VM
05-26 09:41:49.840: W/dalvikvm(11186): threadid=1: thread exiting with uncaught exception (group=0x414f82a0)
05-26 09:41:49.861: E/AndroidRuntime(11186): FATAL EXCEPTION: main
05-26 09:41:49.861: E/AndroidRuntime(11186): java.lang.SecurityException: WifiService: Neither user 10105 nor current process has android.permission.ACCESS_WIFI_STATE.
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Parcel.readException(Parcel.java:1425)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Parcel.readException(Parcel.java:1379)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.net.wifi.IWifiManager$Stub$Proxy.getWifiEnabledState(IWifiManager.java:900)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.net.wifi.WifiManager.getWifiState(WifiManager.java:1078)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.net.wifi.WifiManager.isWifiEnabled(WifiManager.java:1090)
05-26 09:41:49.861: E/AndroidRuntime(11186): at com.example.wifiscanner.MainActivity$1.onClick(MainActivity.java:44)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.view.View.performClick(View.java:4262)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.view.View$PerformClick.run(View.java:17421)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Handler.handleCallback(Handler.java:615)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Looper.loop(Looper.java:137)
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.app.ActivityThread.main(ActivityThread.java:4947)
05-26 09:41:49.861: E/AndroidRuntime(11186): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 09:41:49.861: E/AndroidRuntime(11186): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 09:41:49.861: E/AndroidRuntime(11186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-26 09:41:49.861: E/AndroidRuntime(11186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-26 09:41:49.861: E/AndroidRuntime(11186): at dalvik.system.NativeStart.main(Native Method)
05-26 09:41:49.941: D/dalvikvm(11186): GC_CONCURRENT freed 203K, 15% free 6966K/8135K, paused 12ms+9ms, total 73ms
05-26 09:42:00.201: I/Process(11186): Sending signal. PID: 11186 SIG: 9
My Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifiscanner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.wifiscanner.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>
As soon as I hit the scan button its stop working. If anybody can help me out I would be thankful.

You logcat clearly said
java.lang.SecurityException: WifiService: Neither user 10105 nor current process has android.permission.ACCESS_WIFI_STATE.
05-26 09:41:49.861: E/AndroidRuntime(11186): at android.os.Parcel.readException(Parcel.java:1425)
Add android.permission.ACCESS_WIFI_STATE in your manifest.xml

You need to add the following permissions to the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
These need to be added outside the application tag. (Either before or after)
Your manifest file is located in the main folder of the project under the name: AndroidManifest.xml.

Related

Unfortunately your "APP" has stopped working [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
I am new to Android App development and I am having an issue with an app. I have read everything I could find on stackoverflow about the problem and checked my work against the suggestions from the other questions asked.
Thanks for the help.
Unfortunately application has stopped android emulator genymotion
I am trying to run the app on a Samsung Gal 3 tab.
Below are my files
MainActivity.java
package net.androidbootcamp.concerttickets;
import java.text.DecimalFormat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
double costPerTicket = 79.99;
int numberOfTickets;
double totalCost;
String groupChoice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tickets=(EditText)findViewById(R.id.txtTicket);
final Spinner group = (Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.btnCost);
cost.setOnClickListener(new OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
numberOfTickets = Integer.parseInt(tickets.getText().toString());
totalCost = costPerTicket * numberOfTickets;
DecimalFormat currency = new DecimalFormat("$###,###.##");
groupChoice = group.getSelectedItem().toString();
result.setText("Total Cost for" + groupChoice + "is" + currency.format(totalCost));
}
});
}
#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);
}
}
ActivityMain.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="net.androidbootcamp.concerttickets.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/txtTitle"
android:textSize="48sp" />
<EditText
android:id="#+id/txtTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtGroup"
android:layout_below="#+id/textView1"
android:ems="10"
android:hint="#string/txtTickets"
android:inputType="number"
android:textSize="32sp" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/txtGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTicket"
android:layout_centerHorizontal="true"
android:entries="#array/txtGroup"
android:prompt="#string/prompt" />
<Button
android:id="#+id/btnCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtGroup"
android:layout_centerHorizontal="true"
android:text="#string/btnCost"
android:textSize="32sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCost"
android:layout_centerHorizontal="true"
android:textSize="18sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtGroup"
android:layout_alignParentBottom="true"
android:layout_marginBottom="19dp"
android:contentDescription="#string/description"
android:src="#drawable/concert" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.androidbootcamp.concerttickets"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name="net.androidbootcamp.concerttickets.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>
LogCat
08-14 17:19:35.330: D/AndroidRuntime(20330): Shutting down VM
08-14 17:19:35.330: W/dalvikvm(20330): threadid=1: thread exiting with uncaught exception (group=0x41cf0e10)
08-14 17:19:35.340: E/AndroidRuntime(20330): FATAL EXCEPTION: main
08-14 17:19:35.340: E/AndroidRuntime(20330): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.androidbootcamp.concerttickets/net.androidbootcamp.concerttickets.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.access$700(ActivityThread.java:150)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.os.Looper.loop(Looper.java:176)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.main(ActivityThread.java:5279)
08-14 17:19:35.340: E/AndroidRuntime(20330): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 17:19:35.340: E/AndroidRuntime(20330): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 17:19:35.340: E/AndroidRuntime(20330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-14 17:19:35.340: E/AndroidRuntime(20330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-14 17:19:35.340: E/AndroidRuntime(20330): at dalvik.system.NativeStart.main(Native Method)
08-14 17:19:35.340: E/AndroidRuntime(20330): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
08-14 17:19:35.340: E/AndroidRuntime(20330): at net.androidbootcamp.concerttickets.MainActivity.onCreate(MainActivity.java:26)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.Activity.performCreate(Activity.java:5267)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
08-14 17:19:35.340: E/AndroidRuntime(20330): ... 11 more
The LogCat output shows that you aren't using the theme correctly. in particular, it wants you to use a compatibility theme. You can do that by altering/including a theme attribute for the activity in your manifest file.
If you're not actually going to use an action bar, your could also just change the parent of your activity from ActionbarActivity to just Activity like this:
public class MainActivity extends Activity {

The application has stopped unexpectedly . Error-caused by: Java.lang.NullPointerException

main.java
package com.learnactivities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
activity.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=".Main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
</RelativeLayout>
Second.java
package com.learnactivities;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
second.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=".Second" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="#string/nd" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnactivities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learnactivities.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second" />
</application>
</manifest>
I m getting following errors. I can see that error caused by java.lang.NullPointerException. I did my best searching here and there but could not get to the solution.Plz, anyone help me sort this problem out?
08-25 19:08:44.442: D/AndroidRuntime(276): Shutting down VM
08-25 19:08:44.442: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-25 19:08:44.472: E/AndroidRuntime(276): FATAL EXCEPTION: main
08-25 19:08:44.472: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnactivities/com.learnactivities.Main}: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-25 19:08:44.472: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at com.learnactivities.Main.onCreate(Main.java:19)
08-25 19:08:44.472: E/AndroidRuntime(276): at `android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)`
`08-25 19:08:44.472: E/AndroidRuntime(276): at` `android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)`
08-25 19:08:44.472: E/AndroidRuntime(276): ... 11 more
R.id.button1 is declared inside second.xml, but you are looking for it inside activity.xml. So in Main, when you do
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
findViewById is returning null
Try this in your main class..
TextView b = (TextView ) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
and also in manifest change like below line
<activity android:name=".Second" />

Unfortunately app has stopped (At first activity)

I've read a lot of threads about this "common" problem developing Android apps.
I've also noticed that it all depends on various problem, some from the manifest, some from Java classes.
I'll paste my code here, please if someone see something weird, please tell me.
As you can see, I'm using the MVC-pattern. Please tell me if you need the Model & Controller java class too.
-----ENTER VIEW JAVA CLASS-----
package kalle.jack.gui_tictactoe_android;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class EnterView extends Activity {
private Model _model;
private Controller _controller;
public EnterView (Model aModel, Controller aController) {
_model = aModel;
_controller = aController;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_view);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_view, menu);
return true;
}
public void startClicked () {
TextView s = (TextView) findViewById(R.id.text1);
String player1 = (String) s.getText();
TextView t = (TextView) findViewById(R.id.text2);
String player2 = (String) t.getText();
Button b = (Button) findViewById(R.id.button1);
b.setId(10);
int a = b.getId();
_controller.setNames(player1, player2);
_controller.buttonClicked(a);
}
}
------XML MANIFEST-------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kalle.jack.gui_tictactoe_android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true">
<activity
android:name="kalle.jack.gui_tictactoe_android.EnterView"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="kalle.jack.gui_tictactoe_android.PlayView"
android:label="#string/title_activity_play_view" >
</activity>
</application>
</manifest>
----------XML activity_enter_view-----------
<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=".EnterView" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:text="Enter your names below..."
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="85dp"
android:text="Start Game"
android:onClick="startClicked"/>
<EditText
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Player 1" />
<EditText
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPersonName"
android:text="Player 2" >
<requestFocus />
</EditText>
</RelativeLayout>
-----FROM LOGCAT --------
05-26 06:33:24.460: E/AndroidRuntime(943): FATAL EXCEPTION: main
05-26 06:33:24.460: E/AndroidRuntime(943): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kalle.jack.gui_tictactoe_android/kalle.jack.gui_tictactoe_android.EnterView}: java.lang.InstantiationException: can't instantiate class kalle.jack.gui_tictactoe_android.EnterView; no empty constructor
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.os.Looper.loop(Looper.java:137)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 06:33:24.460: E/AndroidRuntime(943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-26 06:33:24.460: E/AndroidRuntime(943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-26 06:33:24.460: E/AndroidRuntime(943): at dalvik.system.NativeStart.main(Native Method)
05-26 06:33:24.460: E/AndroidRuntime(943): Caused by: java.lang.InstantiationException: can't instantiate class kalle.jack.gui_tictactoe_android.EnterView; no empty constructor
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.Class.newInstanceImpl(Native Method)
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.Class.newInstance(Class.java:1319)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-26 06:33:24.460: E/AndroidRuntime(943): ... 11 more
Firstly, activities must have empty constructors. The system cannot call or instantiate your activity if it requires parameters. Remove the parameters or do not define a constructor at all.
Remove:
public EnterView (Model aModel, Controller aController) {
_model = aModel;
_controller = aController;
}
The next error you would encounter would be the startClicked() method, as praveenLal said. It should accept a View parameter which will represent the clicked button when the method is called.
Please use public void startClicked (View v){
........
....
}
instead of public void startClicked ().

Android Java: Global variables through subclass; can't launch app?

I've been reading up examples on how to do this, trying to piece together a functioning example from posts on this site and others. I'm sure I'm missing something small, but as a novice to Java, I could use some assistance.
I'm merely trying to create a small example, derived from the automated hello world generated when creating a new project in Eclipse. All I want to do is be able to store a few global variables in a subclass, then reference those values in my main activity. Unfortunately, every time I try to run the app, it crashes "Unfortunately, GeneralTest1 has stopped", and the log cat error is not very helpful.
Quick overview:
GlobalVars class extends Application
In the manifest, android:name has been added to reference the additional GlobalVars class
Within my main activity, I'm initializing the global vars class with getApplicationContext()
Here is everything I've got; any help would be much appreciated!
MainActivity.java
package com.example.generaltest1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
GlobalVars.java
package com.example.generaltest1;
import android.app.Application;
public class GlobalVars extends Application {
String myString = "Some Text";
public String getMyString() {
return myString;
}
public String setMyString(String string) {
this.myString = string;
return myString;
}
}
activity_main.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" >
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">GeneralTest1</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
GeneralTest1 Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.generaltest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:name="GlobalVars">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Log Cat
08-14 09:53:05.546: E/Trace(620): error opening trace file: No such file or directory (2)
08-14 09:53:05.656: D/AndroidRuntime(620): Shutting down VM
08-14 09:53:05.656: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-14 09:53:05.676: E/AndroidRuntime(620): FATAL EXCEPTION: main
08-14 09:53:05.676: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.generaltest1/com.example.generaltest1.MainActivity}: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 09:53:05.676: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.example.generaltest1.MainActivity.<init>(MainActivity.java:10)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstanceImpl(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstance(Class.java:1319)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-14 09:53:05.676: E/AndroidRuntime(620): ... 11 more
You are calling getApplicationContext() in your class definition, which is too early in the activity lifetime and results in a nullpointer exception. Move the assignments to your onCreate() function and it should work as expected.
Please check this article about activities and their lifecycle :
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Your activity should look like this:
public class MainActivity extends Activity {
GlobalVars myVars;
TextView myText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVars = ((GlobalVars)getApplication());
myText = (TextView) findViewById(R.id.myText);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Add these lines in oncreate()..
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);

MapView: Could not find class A referenced from method B

There are 3 buttons on the screen! Start, View Map, Stop
When I click View Map, it should go to a new screen that shows the map! But something goes wrong and the app is getting force closed! I keep getting Could not find class A referenced from method B error. Please please please someone correct it. I've been struck with this for 3 days!!! :'(
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="GPS App"
android:id="#+id/textView1"
android:textSize="40sp"
android:padding="10dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:text="Start"
android:id="#+id/buttonStart"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:text="View Map"
android:id="#+id/buttonMap"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:text="Stop"
android:id="#+id/buttonStop"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
map.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mymap"
android:clickable="true"
android:enabled="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="xxxxxxxxxxxxx"
/>
<LinearLayout
android:id="#+id/myzoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Firstdroid.Gps"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".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>
<service android:permission="android.permission.INTERNET"
android:name=".IntentService" android:enabled="true" />
<activity android:name=".MapViewer" android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
MainActivity.java
package Firstdroid.Gps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* START BUTTON */
Button startButton = (Button) findViewById(R.id.buttonStart);
startButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Log.d("Firstdroid.Gps", "Starting Exploration..");
// Start Exploration
startService(new Intent(MainActivity.this, GPSexp.class));
}
});
/* MAP BUTTON */
Button mapButton = (Button) findViewById(R.id.buttonMap);
mapButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
Log.d("Firstdroid.Gps", "Loading Map..");
// Loading Google Map View
startService(new Intent(MainActivity.this, MapViewer.class));
}
});
/* STOP BUTTON */
Button stopButton = (Button) findViewById(R.id.buttonStop);
stopButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d("Firstdroid.Gps", "Stopping Exploration..");
// Stop Exploration
stopService(new Intent(MainActivity.this, GPSexp.class));
}
});
}
}/* End of MainActivity */
The start and stop buttons refer to GPSexp.class defined in GPSexp.java which works correctly. Problem is with MapViewer.java
MapViewer.java
package Firstdroid.Gps;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MapViewer extends MapActivity {
MapView myMap;
MyLocationOverlay myLocOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initMap();
initMyLocation();
}
private void initMap() {
myMap = (MapView) findViewById(R.id.mymap);
View zoomView = myMap.getZoomControls();
LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
myzoom.addView(zoomView);
myMap.displayZoomControls(true);
}
/**
* Initialises the MyLocationOverlay and adds it to the overlays of the map
*/
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Logcat
06-30 04:25:07.519: WARN/dalvikvm(357): Unable to resolve superclass of LFirstdroid/Gps/MapViewer; (37)
06-30 04:25:07.519: WARN/dalvikvm(357): Link of class 'LFirstdroid/Gps/MapViewer;' failed
06-30 04:25:07.547: ERROR/dalvikvm(357): Could not find class 'Firstdroid.Gps.MapViewer', referenced from method Firstdroid.Gps.MainActivity$2.onClick
06-30 04:25:07.547: WARN/dalvikvm(357): VFY: unable to resolve const-class 8 (LFirstdroid/Gps/MapViewer;) in LFirstdroid/Gps/MainActivity$2;
06-30 04:25:07.559: DEBUG/dalvikvm(357): VFY: replacing opcode 0x1c at 0x000d
06-30 04:25:07.573: DEBUG/dalvikvm(357): VFY: dead code 0x000f-0015 in LFirstdroid/Gps/MainActivity$2;.onClick (Landroid/view/View;)V
06-30 04:25:07.909: INFO/ActivityManager(58): Displayed activity Firstdroid.Gps/.MainActivity: 1828 ms (total 1828 ms)
06-30 04:25:13.180: DEBUG/dalvikvm(129): GC_EXPLICIT freed 670 objects / 38560 bytes in 169ms
06-30 04:25:13.840: DEBUG/Firstdroid.Gps(357): Loading Map..
06-30 04:25:13.850: DEBUG/AndroidRuntime(357): Shutting down VM
06-30 04:25:13.850: WARN/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): FATAL EXCEPTION: main
06-30 04:25:13.880: ERROR/AndroidRuntime(357): java.lang.NoClassDefFoundError: Firstdroid.Gps.MapViewer
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at Firstdroid.Gps.MainActivity$2.onClick(MainActivity.java:40)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.view.View.performClick(View.java:2408)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.view.View$PerformClick.run(View.java:8816)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Handler.handleCallback(Handler.java:587)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:521)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
06-30 04:25:13.900: WARN/ActivityManager(58): Force finishing activity Firstdroid.Gps/.MainActivity
06-30 04:25:14.450: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44fe90c0 Firstdroid.Gps/.MainActivity}
06-30 04:25:15.750: INFO/Process(357): Sending signal. PID: 357 SIG: 9
06-30 04:25:15.841: INFO/ActivityManager(58): Process Firstdroid.Gps (pid 357) has died.
06-30 04:25:15.880: INFO/WindowManager(58): WIN DEATH: Window{4500c988 Firstdroid.Gps/Firstdroid.Gps.MainActivity paused=false}
You added uses-library as child of the activity element... maybe that's the problem.
This is not a standard package in the
Android library. In order to use it,
you must add the following XML
element, as a child of the
application element, in your AndroidManifest.xml file:
<uses-library android:name="com.google.android.maps" />
You're using startService to start your MapViewer activity. Hope that helps

Categories